{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From D:\\anaconda3\\lib\\site-packages\\tensorflow\\python\\compat\\v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "non-resource variables are not supported in the long term\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import tensorflow.compat.v1 as tf\n",
    "tf.disable_v2_behavior()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from PIL import Image\n",
    "import time\n",
    "from ChessBoard import ChessBoard\n",
    "import matplotlib.pyplot as plt\n",
    "%matplotlib inline"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "模型路径"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "model_path = \"./ass_net_3.0\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "加载模型"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "INFO:tensorflow:Restoring parameters from ./ass_net_3.0\\conv_ver_1.ckpt\n",
      "INFO:tensorflow:Restoring parameters from ./ass_net_3.0\\conv_ver_1.ckpt\n",
      "INFO:tensorflow:Restoring parameters from ./ass_net_3.0\\conv_ver_1.ckpt\n",
      "INFO:tensorflow:Restoring parameters from ./ass_net_3.0\\conv_ver_1.ckpt\n",
      "INFO:tensorflow:Restoring parameters from ./ass_net_3.0\\conv_ver_1.ckpt\n",
      "INFO:tensorflow:Restoring parameters from ./ass_net_3.0\\conv_ver_1.ckpt\n",
      "INFO:tensorflow:Restoring parameters from ./ass_net_3.0\\conv_ver_1.ckpt\n",
      "INFO:tensorflow:Restoring parameters from ./ass_net_3.0\\conv_ver_1.ckpt\n",
      "INFO:tensorflow:Restoring parameters from ./ass_net_3.0\\conv_ver_1.ckpt\n",
      "INFO:tensorflow:Restoring parameters from ./ass_net_3.0\\conv_ver_1.ckpt\n"
     ]
    }
   ],
   "source": [
    "sess = []\n",
    "graph = []\n",
    "for i in range(10):\n",
    "    graph.append(tf.Graph())\n",
    "    sess.append(tf.Session(graph = graph[i]))\n",
    "    with graph[i].as_default():\n",
    "        saver =tf.train.import_meta_graph(model_path + '/conv_ver_1.ckpt.meta')\n",
    "        saver.restore(sess[i],tf.train.latest_checkpoint(model_path))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# for i in range(10):\n",
    "#     print(graph[i])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "提取输入输出接口"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "xs = []\n",
    "w1 = []\n",
    "b1=[]\n",
    "w2=[]\n",
    "b2=[]\n",
    "w3=[]\n",
    "b3=[]\n",
    "y = []\n",
    "for i in range(10):\n",
    "    xs.append(graph[i].get_tensor_by_name('xs:0'))\n",
    "    w1.append(graph[i].get_tensor_by_name('w1:0'))\n",
    "    b1.append(graph[i].get_tensor_by_name('b1:0'))\n",
    "    w2.append(graph[i].get_tensor_by_name('w2:0'))\n",
    "    b2.append(graph[i].get_tensor_by_name('b2:0'))\n",
    "    w3.append(graph[i].get_tensor_by_name('w3:0'))\n",
    "    b3.append(graph[i].get_tensor_by_name('b3:0'))\n",
    "    y.append(graph[i].get_tensor_by_name('y:0'))\n",
    "    with graph[i].as_default():\n",
    "        b1_r = tf.truncated_normal(shape=[3,3,1,8], stddev=0.02)\n",
    "        b2_r = tf.truncated_normal(shape=[16], stddev=0.1)\n",
    "        b1[i] = b1[i]+b1_r\n",
    "        b2[i] = b2[i]+b2_r"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "网络变异函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 网络参数大致\n",
    "# i 网络序号  sigma 变异程度\n",
    "def variation(i,sigma=0.002):\n",
    "    with graph[i].as_default():\n",
    "        #卷积层1\n",
    "        sess[i].run(tf.assign(w1[i],sess[i].run(w1[i])+tf.truncated_normal(shape=[3,3,1,8], stddev=sigma)))\n",
    "        sess[i].run(tf.assign(b1[i],sess[i].run(b1[i])+tf.truncated_normal(shape=[8], stddev=0.1)))\n",
    "        #卷积层2\n",
    "        sess[i].run(tf.assign(w2[i],sess[i].run(w2[i])+tf.truncated_normal(shape=[3,3,8,16], stddev=sigma)))\n",
    "        sess[i].run(tf.assign(b2[i],sess[i].run(b2[i])+tf.truncated_normal(shape=[16], stddev=0.1)))\n",
    "        #全连接层\n",
    "        sess[i].run(tf.assign(w3[i],sess[i].run(w3[i])+tf.truncated_normal(shape=[5*5*16, 1],stddev=sigma)))\n",
    "        sess[i].run(tf.assign(b3[i],sess[i].run(b3[i])+tf.truncated_normal(shape=[1,1], stddev=0.1)))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看网络参数的大致范围    为[10e1,10e-2]量级之间"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[[-0.04725843  0.4503509   0.01483895 -0.32782602 -0.17257856\n",
      "    0.18062364  0.01153039 -0.09361976]]\n",
      "\n",
      " [[-0.05912223  0.44804642 -0.01609841 -0.32399443 -0.17313889\n",
      "    0.15727869 -0.01517703 -0.1033792 ]]\n",
      "\n",
      " [[-0.06270165  0.48178342 -0.00884655 -0.31186587 -0.19540529\n",
      "    0.18949273 -0.04235823 -0.12682837]]]\n",
      "[[0.14847764]]\n"
     ]
    }
   ],
   "source": [
    "print(sess[0].run(b1[0][0]))\n",
    "print(sess[0].run(b3[0]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查看sigma对参数范围的影响\n",
    "\n",
    "sigma = 0.1  1e-2 \n",
    "\n",
    "sigma = 0.05 5*1e-4\n",
    "\n",
    "sigma = 0.01 1e-4"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.00052546477\n"
     ]
    }
   ],
   "source": [
    "with tf.Session() as s:\n",
    "    z = tf.Variable(tf.truncated_normal(shape=[1,10000], stddev=0.05))\n",
    "    s.run(tf.global_variables_initializer())\n",
    "    print(s.run(tf.reduce_mean(z)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 查看网络运行速度\n",
    "1000次网络前向传播，2.5秒"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 加载 测试集 标签\n",
    "test_path = \"C:/Users/王欣哲/Desktop/codes/人工智能基础/问题3 进化学习/dataset\"\n",
    "with open(test_path + \"/test.csv\") as f:\n",
    "    test_data = np.loadtxt(f,delimiter=',') \n",
    "    test_len = len(test_data)\n",
    "def get_test_batch(batch_size):\n",
    "    idx = np.random.randint(1,test_len,size=batch_size)\n",
    "    _chess=[]\n",
    "    _label=[]\n",
    "    for i in idx:\n",
    "        _chess.append(test_data[i,0:225])\n",
    "        _label.append(test_data[i,225])\n",
    "    _chess = np.asfarray(_chess)\n",
    "    _label = np.asfarray([_label]).reshape([batch_size,1])\n",
    "    return _chess,_label/1000000"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "t_start = time.time()\n",
    "\n",
    "for j in range(100):\n",
    "    batch = get_test_batch(1)\n",
    "    for i in range(10):\n",
    "        sess[i].run(y[i],feed_dict={xs[i]:batch[0]})\n",
    "        \n",
    "t_end = time.time()\n",
    "print(\"time = \",t_end-t_start)\n",
    "print(\"FPS = \",1.0/(t_end-t_start))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
